using System.Collections.Generic;
using System.Linq;
using Managistics.Core.Infrastructure;
using Managistics.Core.Repositories;
using Managistics.Core.Services;
using Managistics.Model;
using Managistics.Model.ValueObject;
using System;
using Managistics.Utility.Common;

namespace Managistics.Core.Services.Impl
{
    public class EnterInputSheetService : IEnterInputSheetService
    {
        private readonly IInputOtherRepository _inputOtherRepository;
        private readonly IInputSheetOtherDetailRepository _inputSheetOtherDetailRepository;
       

        /// <summary>
        /// EnterInputSheetService constructor
        /// </summary>
        /// <param name="inputRepository"></param>
        /// <param name="purchaseOtherRepository"></param>
        /// <param name="purchaseOtherDetailRepository"></param>
        /// <param name="inputSheetDetailRepository"></param>
        public EnterInputSheetService(IInputOtherRepository inputOtherRepository,
            IInputSheetOtherDetailRepository inputSheetOtherDetailRepository)
        {
            this._inputOtherRepository = inputOtherRepository;
            this._inputSheetOtherDetailRepository = inputSheetOtherDetailRepository;
            
        }
        
        public string saveInputSheet(ViewEnterInputSheetParam p)
        {
            string inputSheetId = "";
            if (string.IsNullOrEmpty(p.InputSheetId))
            {
               
                //New InputSheet
                InputSheetOthers iSheet = new InputSheetOthers();
                iSheet.InputedDate = p.inputDate;
                iSheet.Year = p.inputDate.Year;
                iSheet.Month = p.inputDate.Month;
                iSheet.InputType = p.InputSheetType.TypeId;
                iSheet.SupplierCode = p.Supplier.Id;
                iSheet.CustomerCode = p.Customer.Code;
                iSheet.Department = p.Department;
                iSheet.Description = p.Description;
                iSheet.EmployeeId = "EMP0000000001";
                //iSheet.IsDeleted;
                iSheet.CreatedDate = DateTime.Now;
                iSheet.UpdatedDate = DateTime.Now;

                iSheet.InputSheetOtherDetail = new List<InputSheetOtherDetail>();
                foreach (InputSheetDetailVO vo in p.listDetail)
                {
                    if (!string.IsNullOrEmpty(vo.GoodCode) && !string.IsNullOrEmpty(vo.GoodName)
                        && vo.Quantity > 0)
                    {
                        InputSheetOtherDetail detail = new InputSheetOtherDetail();
                        //detail.CurrentPrice = ;
                        detail.Good = vo.Good;
                        detail.GoodName = vo.GoodName;
                        //detail.Id = ;
                        detail.Inputsheetothers = iSheet;
                        //detail.Note = ;
                        detail.Quantity = int.Parse(vo.Quantity.ToString());
                        //iSheet.InputSheetOtherDetail.Add(detail);
                        iSheet.AddInputSheetOtherDetail(detail);
                    }
                    
                }
                
                this._inputOtherRepository.Insert(iSheet);
                                
            }
            else
            {
                //Edit InputSheet
            }

            return inputSheetId;
        }
    }
}